﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using DataAccess;

namespace Project
{
    /// <summary>
    /// Interaction logic for GrantDirectorPermissionWindows.xaml
    /// </summary>
    public partial class GrantDirectorPermissionWindows : Window
    {
        private bool _result;

        public GrantDirectorPermissionWindows()
        {
            InitializeComponent();
            AddHandler(Keyboard.KeyDownEvent, (KeyEventHandler)KeyPressed);
        }
        private void KeyPressed(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                if (this.FindName("btnLogIn") != null && btnLogIn.IsVisible)
                    btnLogIn_Click(null, null);
            }
        }

        private void WaitForResult()
        {
            Thread.Sleep(1000);
            lblMessage.Dispatcher.Invoke(new System.Action(() =>
                                                               {lblMessage.Visibility = Visibility.Visible;
                                                               this.DialogResult = _result;
                                                               }));
        }

        private void btnLogIn_Click(object sender, RoutedEventArgs e)
        {
           bool res = UserControllerAndDataAccess.GrantPermissionFromDirectorAccount(tbUsername.Text, tbPassword.Password);
           if (res)
           {
               btnLogIn.Visibility = Visibility.Collapsed;
               btnCancel.Visibility = Visibility.Collapsed;
               lblUsername.Visibility = Visibility.Collapsed;
               lblPassword.Visibility = Visibility.Collapsed;
               tbPassword.Visibility = Visibility.Collapsed;
               tbUsername.Visibility = Visibility.Collapsed;
               lblMessage.Content = "Permiso concedido!";
               lblMessage.Foreground = Brushes.DeepSkyBlue;
               lblMessage.Visibility = Visibility.Visible;
               this._result = true;    
           }
           else
           {
               btnLogIn.Visibility = Visibility.Collapsed;
               btnCancel.Visibility = Visibility.Collapsed;
               lblUsername.Visibility = Visibility.Collapsed;
               lblPassword.Visibility = Visibility.Collapsed;
               tbPassword.Visibility = Visibility.Collapsed;
               tbUsername.Visibility = Visibility.Collapsed;
               lblMessage.Content = "Permiso denegado!";
               lblMessage.Foreground = Brushes.Red;
               lblMessage.Visibility = Visibility.Visible;
               this._result = false;
           }
            Thread hThread = new Thread(WaitForResult);
            hThread.Start();
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
            this.Close();
        }
        
    }
}
